var msg = require('../../config/system_message');
var _ = require('lodash');

module.exports = exports;

/**
 * 账户表
 */
exports.Account = require('./account');

/**
 * 医生账户最新的抢答时间表
 */
exports.AccountAnswer = require('./account_answer');

/**
 * 收藏表
 */
exports.Collection = require('./collection');

/**
 * 用户病情备注表
 */
exports.Condition = require('./condition');

/**
 * 用户评价信息表
 */
exports.Evaluation = require('./evaluation');

/**
 * 用户组表
 */
exports.Group = require('./group');

/**
 * 账户登录信息表
 */
exports.LoginInfo = require('./login_info');

/**
 * 消息表
 */
exports.Message = require('./message');

/**
 * 问题表
 */
exports.Question = require('./question');

/**
 * 问题类别表
 */
exports.QuestionClassification = require('./question_classification');

/**
 * 对话记录表
 */
exports.SessionLog = require('./session_log');

/**
 * socket缓存表
 */
exports.SocketSession = require('./socket_session');

/**
 * 话术表
 */
exports.SpeakArt = require('./speak_art');

/**
 * 话术分类表
 */
exports.SpeakArtClassification = require('./speak_art_classification');

/**
 * 用户表
 */
exports.User = require('./user');

/**
 * 用户与组的关系表
 */
exports.UserGroupRela = require('./user_group_rela');

//
_.forEach(exports, function (model) {
    _.assign(model, {
        createDoc: function (doc, callback) {
            model.create(doc, function (err, document) {
                if (err) {
                    console.error('create %s error: %s', model.modelName, err);
                    callback && callback({code: msg.CODE.SYSTEM_ERROR, message: msg.ERR_MSG.SAVE_DATA});
                } else {
                    console.log('create %s: %j', model.modelName, document);
                    callback && callback(null, document);
                }
            });
        },
        removeDocs: function (conditions, callback) {
            if ('function' === typeof conditions) {
                callback = conditions;
                conditions = {};
            }
            model.remove(conditions, function (err, raw) {
                if (err) {
                    console.error('remove %s error: ', model.modelName, err);
                    callback && callback({code: msg.CODE.SYSTEM_ERROR, message: msg.ERR_MSG.REMOVE_DATA});
                } else {
                    console.log('remove %s result: ', model.modelName, raw.result);
                    callback && callback();
                }
            });
        },
        updateDocs: function (conditions, update, options, callback) {
            if ('function' == typeof options) {
                callback = options;
                options = null;
            }

            //
            if (!update.updatedAt) {
                update.updatedAt = Date.now();
            }

            model.update(conditions, {$set: update}, options, function (err, raw) {
                if (err) {
                    console.error('update %s error: ', model.modelName, err);
                    callback && callback({code: msg.CODE.SYSTEM_ERROR, message: msg.ERR_MSG.UPDATE_DATA});
                } else {
                    console.log('update %s result: ', model.modelName, raw.result);
                    callback && callback();
                }
            });
        },
        countDocs: function (conditions, callback) {
            if ('function' === typeof conditions) {
                callback = conditions;
                conditions = {};
            }
            model.count(conditions, function (err, count) {
                if (err) {
                    console.error('count %s error: %s', model.modelName, err);
                    callback && callback({code: msg.CODE.SYSTEM_ERROR, message: msg.ERR_MSG.GET_DATA});
                } else {
                    callback && callback(null, count);
                }
            });
        },
        findDocs: function (conditions, fields, options, callback) {
            if ('function' == typeof conditions) {
                callback = conditions;
                conditions = {};
                fields = null;
                options = null;
            } else if ('function' == typeof fields) {
                callback = fields;
                fields = null;
                options = null;
            } else if ('function' == typeof options) {
                callback = options;
                options = null;
            }
            model.find(conditions, fields, options, function (err, docs) {
                if (err) {
                    console.error('find %s error: %s', model.modelName, err);
                    callback && callback({code: msg.CODE.SYSTEM_ERROR, message: msg.ERR_MSG.GET_DATA});
                } else {
                    var results = docs || [];
                    callback && callback(null, results);
                }
            });
        },
        findOneDoc: function (conditions, fields, options, callback) {
            if ('function' == typeof options) {
                callback = options;
                options = null;
            } else if ('function' == typeof fields) {
                callback = fields;
                fields = null;
                options = null;
            } else if ('function' == typeof conditions) {
                callback = conditions;
                conditions = {};
                fields = null;
                options = null;
            }
            model.findOne(conditions, fields, options, function (err, doc) {
                if (err) {
                    console.error('find %s error: %s', model.modelName, err);
                    callback && callback({code: msg.CODE.SYSTEM_ERROR, message: msg.ERR_MSG.GET_DATA});
                } else {
                    callback && callback(null, doc);
                }
            });
        }
    });
});
